Fix PetabStrPrinter for non-integer rational exponents#489
Merged
dweindl merged 2 commits intoJun 22, 2026
Conversation
A non-integer Rational exponent (e.g. a square root, exponent 1/2) is a sympy Atom but prints as the multi-token "1/2", so PetabStrPrinter emitted `sqrt(a)` as the unparenthesized `a ^ 1/2`. Since `^` binds tighter than `/`, that re-parses as `(a^1)/2 = a/2` -- a silent round-trip corruption (`petab_math_str(sympify_petab(...))` is not the identity for square roots). The `not exp.is_Atom` guard added in PEtab-dev#421 covers non-atomic exponents but not this atomic-yet-multi-token case; parenthesize a non-integer rational exponent explicitly, so `petab_math_str(sqrt(a)) == "a ^ (1/2)"`, which re-parses correctly. Integer powers and the PEtab-dev#421 cases are unchanged.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #489 +/- ##
=======================================
Coverage 75.29% 75.29%
=======================================
Files 62 62
Lines 6946 6946
Branches 1229 1229
=======================================
Hits 5230 5230
Misses 1245 1245
Partials 471 471 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
petab_math_str(sympify_petab(x))is not the identity for square roots and other non-integer rational exponents. For example,petab_math_str(sqrt(a))returnsa ^ 1/2, whichsympify_petabre-parses as(a^1)/2 = a/2. This is a silent mangling of the expression.The cause is that a non-integer
Rationalexponent, such as1/2, is a sympyAtombut prints as the multi-token string1/2, so thenot exp.is_Atomguard added in #421 does not parenthesize it. Since^binds tighter than/, the unparenthesizeda ^ 1/2then parses as(a^1)/2.This fix parenthesizes a non-integer rational exponent explicitly, so
petab_math_str(sqrt(a)) == "a ^ (1/2)", which re-parses correctly. Integer powers and the non-atomic base/exponent cases from #421 are unchanged.